package lifo.GammaTaf.GammaPomset;

import lifo.GammaTaf.GammaPomset.GammaPomset.Action;
import lifo.GammaTaf.GammaPomset.GammaPomset.Serie;
import lifo.GammaTaf.GammaPomset.GammaPomset.Star;
import lifo.GammaTaf.GammaPomset.GammaPomset.Sum;
import lifo.GammaTaf.SerieParallel.SerieParallel;

public class GammaPomsetToDAGPomset implements VisitorGammaRegularExpression{
	private  StringBuffer graphe = new StringBuffer();
	String debut = "";
	String fin = "";
	
	static public String eval(GammaPomset exp) {
		GammaPomsetToDAGPomset visitor = new GammaPomsetToDAGPomset();
		visitor.graphe.append("digraph system {\n");
		exp.accepteVisiteur(visitor);

		visitor.graphe.append("debut" + " [label=\"\",shape=none]\n");
		visitor.graphe.append("debut" + " -> " + visitor.debut + "\n");
		visitor.graphe.append("fin" + " [label=\"\",shape=none]\n");
		visitor.graphe.append(visitor.fin + " -> " + "fin" + "\n");

		visitor.graphe.append("}\n");
		return visitor.graphe.toString();
	}

	public String eval(String exp) {
		graphe.append("digraph system {\n");
		String[] elements= exp.split("\\)\\(");
		Action[] actions= new Action[elements.length];
		for(int i=elements.length; i>0;i--){
			actions[i-1]= new Action(elements[i-1]);
			graphe.append("N" + actions[i-1].hashCode() + " [label=" + actions[i-1].arg +",shape=none]\n");
		}
		
		for(int i= actions.length; i>0; i--){
			boolean trouveTout=false;
			int j=i-1;
			while(!trouveTout && j>0){
				if(actions[i-1].alpha.containsAll(actions[j-1].beta)){
					graphe.append("N" + actions[j-1].hashCode()+ " -> " + "N" + actions[i-1].hashCode() + "\n");
					actions[i-1].alpha.removeAll(actions[j-1].beta);
					if(actions[i-1].alpha.isEmpty())
						trouveTout=true;
				}
				j--;
			}
		}
		graphe.append("}\n");
		return graphe.toString();
	}

	public void visiteAction(Action action) {
		// TODO Auto-generated method stub
		
	}

	public void visiteSum(Sum sum) {
		// TODO Auto-generated method stub
		
	}

	public void visiteStar(Star star) {
		// TODO Auto-generated method stub
		
	}

	public void visiteSerie(Serie serie) {
		// TODO Auto-generated method stub
		
	}

}
